// components/timeSelector/timeSelect.js
Component({
    /**
     * 组件的属性列表
     */
    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {
        dateList: [{
            week: '周一',
            date: ''
        }, {
            week: '周二',
            date: ''
        }, {
            week: '周三',
            date: ''
        }, {
            week: '周四',
            date: ''
        }, {
            week: '周五',
            date: ''
        }, {
            week: '周六',
            date: ''
        }, {
            week: '周日',
            date: ''
        }],
        // 初始日期or设置日期
        date: "",
        // 选中索引
        checkIndex: 0,
        scrollLeft: 0
    },
    lifetimes: {
        created() {

        },
        ready() {
            var date = new Date()
            date = this.format(date.getTime())
            this.triggerEvent("date", date)
            this.render(date)
            this.setData({
                date
            })
        },
    },

    /**
     * 组件的方法列表
     */
    methods: {
        format(timeStamp) {
            const date = new Date(timeStamp)
            return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()
        },
        // 从picker获取时间：YYYY-MM-DD
        getTime(e) {
            let {
                value
            } = e.detail
            console.log("changer", value);
            this.triggerEvent("date", value)
            this.render(value)
        },
        clickTab(e) {
            let {
                dateList
            } = this.data
            let {
                index
            } = e.currentTarget.dataset
            let date = dateList[index].year + '-' + dateList[index].date
            this.render(date)
            this.triggerEvent("date", date)
            this.setData({
                date
            })
        },
        render(formatDate) {
            let {
                dateList
            } = this.data
            let [year, month, day] = formatDate.split("-").map(item => Number(item))
            const date = new Date()
            date.setFullYear(year)
            date.setMonth(month - 1)
            date.setDate(day)
            let reduce = this.getDay(date)
            // 将日期重置为周一的前一天，也就是上周日
            date.setDate(day - reduce - 1)
            dateList = dateList.map((item, index) => {
                date.setDate(date.getDate() + 1)
                item.date = this.format(date.getTime()).split("-").map(item => item.length == 1 ? '0' + item : item).slice(1).join("-")
                item.year = date.getFullYear()
                return item
            })
            this.scrollTo(reduce)
            this.setData({
                dateList,
                checkIndex: reduce
            })
        },
        getDay(date) {
            let day = date.getDay()
            if (day == 0) {
                return 6
            }
            return day - 1
        },
        scrollTo(step) {
            wx.getSystemInfo({
                success: (result) => {
                    let temp = Math.ceil((result.windowWidth - 50) / 5)
                    let scrollLeft = 0
                    if (step > 3) {
                        scrollLeft = temp * step
                    }
                    this.setData({
                        scrollLeft
                    })
                },
            })
        }
    }
})